import traceback

from fastapi import APIRouter, Depends

from app.api.response import Response, response_docs
from app.business.session_msg import (
    SessionMsgDetailBiz, SessionMsgListBiz, SessionMsgUpdateBiz, SessionMsgDeleteBiz, MsgDetailBiz,
)
from app.api.status import Status
from app.datatype.session_msg import SessionMsgListMdl
from app.initializer import g
from app.middleware.auth import JWTUser, get_current_user

session_msg_router = APIRouter()


# @session_msg_router.get(
#     path="/session_msg/{session_msg_id}",
#     summary="session_msgDetail",
#     responses=response_docs(
#         model=SessionMsgDetailBiz,
#     ),
# )
# async def detail(
#         session_msg_id: str,
#         current_user: JWTUser = Depends(get_current_user),
# ):
#     try:
#         session_msg_biz = SessionMsgDetailBiz(id=session_msg_id)
#         data = await session_msg_biz.detail()
#         if not data:
#             return Response.failure(msg="未匹配到记录", status=Status.RECORD_NOT_EXIST_ERROR)
#     except Exception as e:
#         g.logger.error(traceback.format_exc())
#         return Response.failure(msg="session_msgDetail失败", error=e)
#     return Response.success(data=data)

@session_msg_router.post(
    path="/session_msg/list",
    summary="获取聊天记录",
    responses=response_docs(
        model=SessionMsgListMdl,
        is_listwrap=True,
    ),
)
async def lst(
        session_msg_biz: SessionMsgListBiz,
        current_user: JWTUser = Depends(get_current_user)
):
    try:
        data = await session_msg_biz.lst()
        if not data:
            return Response.failure(msg="未匹配到记录", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="session_msgList失败", error=e)
    return Response.success(data=data)

#修改聊天记录
@session_msg_router.put(
    path="/session_msg/update",
    summary="修改聊天记录",
    responses=response_docs(data={
        "id": int,
    }),
)
async def update(
        session_msg_biz: SessionMsgUpdateBiz,
        current_user: JWTUser = Depends(get_current_user)
):
    try:
        session_id = await session_msg_biz.update()
        if not session_id:
            return Response.failure(msg="更新失败", status=Status.RECORD_EXISTS_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="更新失败", error=e)
    return Response.success(data={"id": session_id})

@session_msg_router.delete(
    path="/session_msg/delete",
    summary="删除聊天记录",
    responses=response_docs(data={
        "id": int,
    }),
)
async def delete(
        session_msg_biz: SessionMsgDeleteBiz,
        current_user: JWTUser = Depends(get_current_user)
):
    try:
        session_id = await session_msg_biz.delete()
        if not session_id:
            return Response.failure(msg="删除失败", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="删除失败", error=e)
    return Response.success(data={"id": session_id})

@session_msg_router.get(
    path="/session_msg/{id}",
    summary="通过id获取聊天记录详情",
    responses=response_docs(
        model=SessionMsgListMdl,
    ),
)
async def detail(
        session_msg_id: str,
        current_user: JWTUser = Depends(get_current_user)
):
    try:
        session_msg_biz = MsgDetailBiz(id=session_msg_id)
        data = await session_msg_biz.detail()
        if not data:
            return Response.failure(msg="未匹配到记录", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="userDetail失败", error=e)
    return Response.success(data=data)
